package com.jing.ads.activity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;

import com.jing.ads.application.MyApplication;
import com.jing.ads.handler.GetAppsHandler;
import com.jing.ads.model.PushAppInfo;
import com.jing.ads.net.RequestListener;
import com.jing.ads.util.CacheManager;
import com.jing.ads.util.SQLUtil;
import com.jing.ads.view.MainView;

public class ActivityMain extends Activity {

	private static final String NOTIFY_APP_PROGRESS = "com.jing.ads.notify.progress";
	private static final String NOTIFY_APP_COMPLETE = "com.jing.ads.notify.complete";
	private static final String NOTIFY_APP_CANCEL = "com.jing.ads.notify.cancel";

	private MainView view;
	private Handler handler;
	private NotifyReceiver receiver;
	private MyApplication app;
	private PushAppInfo info;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		handler = new Handler();
		app = (MyApplication) this.getApplication();
		IntentFilter filter = new IntentFilter();
		filter.addAction(NOTIFY_APP_COMPLETE);
		filter.addAction(NOTIFY_APP_PROGRESS);
		filter.addAction(NOTIFY_APP_CANCEL);
		receiver = new NotifyReceiver();
		this.registerReceiver(receiver, filter);
		view = new MainView(this);
		setContentView(view);
		onRequestApps();
	}

	@Override
	protected void onPause() {
		super.onPause();
		app.setShow(false);
	}

	@Override
	protected void onResume() {
		super.onResume();
		app.setShow(true);
		if (info != null) {
			setData();
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (receiver != null) {
			this.unregisterReceiver(receiver);
		}
	}

	private void setData() {
		handler.post(new Runnable() {

			@Override
			public void run() {
				SQLUtil.checkApps(ActivityMain.this, info);
				view.setData(info);
			}
		});
	}
	
	class NotifyReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if (NOTIFY_APP_PROGRESS.equals(action)) {
				String pName = intent.getStringExtra("pName");
				int progress = intent.getIntExtra("current", 0);
				int total = intent.getIntExtra("total", 0);
				view.setProgress(pName, progress, total);
			} else if (NOTIFY_APP_COMPLETE.equals(action)) {
				String pName = intent.getStringExtra("pName");
				String path = intent.getStringExtra("file");
				view.setComplete(pName, path);
			} else if (NOTIFY_APP_CANCEL.equals(action)) {

			}
		}

	}

	public void onRequestApps() {
		final GetAppsHandler get = new GetAppsHandler(this);
		String key = get.initUrl();
		get.onRequest(new RequestListener() {

			@Override
			public void onError(Object data) {
				finish();
			}

			@Override
			public void onCallBack(Object error) {
				info = (PushAppInfo) get.getData();
				if (info.getAppsList().isEmpty()) {
					finish();
				}else {
					setData();
				}
			}
		}, CacheManager.getInstance(this).hasCache(key));
	}

}
